package com.trkj.service.wxy;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.trkj.dao.wxy.ExamArrangeDao;
import com.trkj.dao.wxy.ExamDao;
import com.trkj.dao.wxy.ExamSiteDao;
import com.trkj.pojo.ExamArrange;
import com.trkj.pojo.ExamPaper;
import com.trkj.pojo.ExamSite;
import com.trkj.service.ServiceException;
import com.trkj.vo.wxy.ExamSitePageVo;

@Service
@Transactional(rollbackFor = Exception.class)
public class ExamSiteService {
	@Autowired
	private ExamDao examDao;
	@Autowired
	private ExamArrangeDao examArrangeDao;
	@Autowired
	private ExamSiteDao examSiteDao;
	
	/**
	 * 查询考试现场管理所有的记录分页
	 */
	public List<ExamSitePageVo> queryByExamSiteKsxcgl(int page,int rows){
		List<ExamSite> list = examSiteDao.queryByExamSiteKsxcgl(new PageRequest(page-1, rows));
		List<ExamSitePageVo> listExamArrangePageVo = new ArrayList<>();
		if(list != null){
			for(ExamSite objs: list){
				ExamSitePageVo examSitePageVo = new ExamSitePageVo();
				if(objs.getExamPaper()!=null){
					ExamPaper e =  objs.getExamPaper();
					examSitePageVo.setEpTotal(e.getEpTotal());
					examSitePageVo.setEpStarttime(e.getEpStarttime());
					examSitePageVo.setEpEndtime(e.getEpEndtime());
					examSitePageVo.setEpShichang(e.getEpShichang());
					examSitePageVo.setClassSjName(e.getClassSjName());
				}
				ExamArrange r = new ExamArrange();
				
				examSitePageVo.setArrangeUnifyEspecial(r.getArrangeUnifyEspecial());
				examSitePageVo.setArrangeNumberExaminee(r.getArrangeNumberExaminee());
				examSitePageVo.setArrangeNumberAdministrator(r.getArrangeNumberAdministrator());
				examSitePageVo.setArrangeAddress(r.getArrangeAddress());
				examSitePageVo.setArrangeRemark(r.getArrangeRemark());
				examSitePageVo.setArrangeStart(r.getArrangeStart());
				
				ExamSite s = objs.getExamPaper().getExamSites().get(0);
				examSitePageVo.setSiId(s.getSiId());
				examSitePageVo.setSiName(s.getSiName());
				
				listExamArrangePageVo.add(examSitePageVo);
			}
		}
		return listExamArrangePageVo;
		
	}
	
	/**
	 * 查询今日考试所有的记录分页
	 */
	public List<ExamSitePageVo> queryByExamSiteJrks(int page,int rows){
		List<ExamSite> list = examSiteDao.queryByExamSiteJrks(new PageRequest(page-1, rows));
		List<ExamSitePageVo> listExamArrangePageVo = new ArrayList<>();
		System.out.println(list.size());
		if(list != null){ 
			for(ExamSite objs: list){
				ExamSitePageVo examSitePageVo = new ExamSitePageVo();
				
				if(objs.getExamPaper()!=null){
					ExamPaper e = objs.getExamPaper();
					examSitePageVo.setClassSjName(e.getClassSjName());
					examSitePageVo.setEpState(e.getEpState());
				}
				ExamArrange r = new ExamArrange();
				List list2 = objs.getExamPaper().getExamArranges();
				examSitePageVo.setArrangeName(r.getArrangeName());
				
				ExamSite s = objs.getExamPaper().getExamSites().get(0);
				
				long nd = 1000 * 24 * 60 * 60;
				long nh = 1000 * 60 * 60;
				long nm = 1000 * 60;
				long ns = 1000;
				long sy=s.getTurnTime().getTime();
				long sd=new Date().getTime();
				long syd=sy-sd;
				long day=syd/nd;
				long sysj=syd% nd%nh/nm+day;
				examSitePageVo.setTurnTime(s.getTurnTime());
				examSitePageVo.setTestTime(s.getTestTime());
				examSitePageVo.setTimeRemaining(new BigDecimal(sysj));
				examSitePageVo.setNotScore(s.getNotScore());
				
				listExamArrangePageVo.add(examSitePageVo);
			}
		}
		return listExamArrangePageVo;
		
	}
	
	/**
	 * 获取总记录数
	 * @return
	 */
	public long empByPageCount(){
		return examSiteDao.count();
		
	}
	
	/**
	 * 删除
	 * @param siId
	 * @throws ServiceException
	 */
	public void ksxcgl_delete(Long siId) throws ServiceException{
		try {
			examSiteDao.delete(siId);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			throw new ServiceException(e);
		}
		
	}
}
